package kr.hkit.powerjava.chapter26.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

import kr.hkit.powerjava.chapter26.dao.EmployeeDAO;
import kr.hkit.powerjava.chapter26.dto.Department;
import kr.hkit.powerjava.chapter26.dto.Employee;

public class EmployeeService extends AbsService<Employee>{
	private ArrayList<Employee> list;
	public EmployeeService(Scanner sc, Connection connection) {
		super(sc, connection);
		dao= new EmployeeDAO(connection);
	}

	@Override
	public Employee inputItemInfo(boolean gubun) {
		System.out.println("사원번호 사원명");
		Employee emp= new Employee(sc.nextInt(),sc.next().trim());
		if(gubun){
			System.out.println("직책 상급자번호 연봉 번호");
			String empName=sc.next();
			emp.setEmpName(empName);
			String title=sc.next();
			emp.setTitle(title);
			int manager=sc.nextInt();
			emp.setManager(manager);
			int salary=sc.nextInt();
			emp.setSalary(salary);
			int dno=sc.nextInt();
			emp.setDno(dno);
		}
		return emp;
	}

	@Override
	public void prnSubMenu() {
		System.out.println("1.번호순 2.이름순 3.직책순 4.직속상사분 5.연봉순 6.부서번호순");
		try {
			list=dao.listItems();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int res=sc.nextInt();
		switch (res) {
			case 1:
				prnItemList(new Comparator<Employee>() {
					@Override
					public int compare(Employee o1, Employee o2) {
						return o1.getEmpNo()-o2.getEmpNo();
					}
				});
				break;
			case 2:
				prnItemList(new Comparator<Employee>() {
					@Override
					public int compare(Employee o1, Employee o2) {
						return o1.getEmpName().hashCode()-o2.getEmpName().hashCode();
					}
				});
				break;
			case 3:
				prnItemList(new Comparator<Employee>() {
					@Override
					public int compare(Employee o1, Employee o2) {
						return o1.getTitle().hashCode()-o2.getTitle().hashCode();
					}
				});
				break;
			case 4:
				prnItemList(new Comparator<Employee>() {
					@Override
					public int compare(Employee o1, Employee o2) {
						return o1.getManager()-o2.getManager();
					}
				});
				break;
			case 5:
				prnItemList(new Comparator<Employee>() {
					@Override
					public int compare(Employee o1, Employee o2) {
						return o1.getSalary()-o2.getSalary();
					}
				});
				break;
			case 6:
				prnItemList(new Comparator<Employee>() {
					@Override
					public int compare(Employee o1, Employee o2) {
						return o1.getDno()-o2.getDno();
					}
				});
				break;
			default:
				prnItemList(null);
		}
	}
	private void prnItemList(Comparator<Employee> comparator) {
		if (comparator == null){
			Collections.sort(list);
		}else{
			Collections.sort(list, comparator);
		}
		prnlist(list);
	}

	private void prnlist(ArrayList<Employee> list) {
		for(Employee e:list){
			System.out.println(e);
		}
	}

}
